package com.hzit.manager.service.impl;

import com.hzit.manager.entity.SpecificationOptionEntity;
import com.hzit.manager.entity.group.Specification;
import com.hzit.manager.service.SpecificationOptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzit.common.utils.PageUtils;
import com.hzit.common.utils.Query;

import com.hzit.manager.dao.SpecificationDao;
import com.hzit.manager.entity.SpecificationEntity;
import com.hzit.manager.service.SpecificationService;


@Service("specificationService")
public class SpecificationServiceImpl extends ServiceImpl<SpecificationDao, SpecificationEntity> implements SpecificationService {
    @Autowired
    private SpecificationOptionService optionService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpecificationEntity> page = this.page(
                new Query<SpecificationEntity>().getPage(params),
                new QueryWrapper<SpecificationEntity>()
        );

        return new PageUtils(page);
    }

    //1. 添加规格及规格选项列表
    @Override
    public void save(Specification specification) {
        //第一步：添加规格
        //1.1 得到规格名字
        String specName = specification.getSpecName();
        //1.2 定义规格对象
        SpecificationEntity spec = new SpecificationEntity();
        spec.setSpecName(specName);
        //1.3 添加规格
        this.save(spec);

        //第二步：添加规格选项
        List<SpecificationOptionEntity> options = specification.getOptions();
        //2.1 遍历规格选项
        for (SpecificationOptionEntity option : options) {
            //2.2 为规格选项指定规格id
            option.setSpecId(spec.getId());
            //2.3 添加每个规格选项
            optionService.save(option);
        }
    }

    //3. 根据规格id查询规格及规格选项列表
    @Override
    public Specification findById(String id) {
        //3.1 查询出规格对象
        SpecificationEntity spec = this.getById(id);
        //3.2 根据规格的id，查询规格选项表格中的对应的规格选项列表
        List<SpecificationOptionEntity> options = optionService.list(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id", id));
        //3.3 返回规格组合对象
        return new Specification(id,spec.getSpecName(),options);
    }

    //4. 修改规格及规格选项的值
    @Override
    public void update(Specification specification) {
        //4.1 首先，修改规格
        SpecificationEntity spec = new SpecificationEntity();
        spec.setId(specification.getId());
        spec.setSpecName(specification.getSpecName());
        this.updateById(spec);

        //4.2 修改规格选项
        //4.2.1 根据规格id这个外键来删除规格选项列表
        optionService.remove(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id", specification.getId()));
        //4.2.2 再执行添加操作
        //① 设置规格选项的规格id这个外键值
        List<SpecificationOptionEntity> options = specification.getOptions();
        for (SpecificationOptionEntity option : options) {
            option.setSpecId(specification.getId());
        }
        //② 进行批量添加
        optionService.saveBatch(options);

    }

}